package com.webdev.newtech.controller;

import com.webdev.newtech.config.JwtConfig;
import com.webdev.newtech.domain.SysUser;
import com.webdev.newtech.service.ISysUserService;
import com.webdev.newtech.utils.AjaxResult;
import com.webdev.newtech.utils.MD5Util;
import com.webdev.newtech.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
public class AuthController {

    @Resource
    private JwtConfig jwtConfig ;
    @Autowired
    RedisUtil redisUtil;

    @Autowired
    ISysUserService userService;



    @PostMapping("login")
    public AjaxResult login(@RequestBody Map dataMap) {
        System.out.println(dataMap);
        String loginName= (String) dataMap.get("loginName");
        String password= (String) dataMap.get("password");
//        String captcha, String uuid;
//        SysUser user = new SysUser();
//        user.setLoginName(loginName);
//        List<SysUser> list = userService.getUserList(user);
//        if(list.size()==1){
//
//        }

//         c从数据库查询 加盐 加 salt = jhfsdkjfhsdkj
        String realName = "admin";
        String realPass = MD5Util.MD5("123456");
        System.out.println("123456经过md5加密后:" + realPass);
//        从缓存中获取
//        String realCaptcha = redisUtil.get(uuid).toString();
        if (loginName == null || loginName.trim().equals("")) {
            return AjaxResult.error("请输入用户名，");
        }

        //判断用户是否存在
        if (loginName.equals(realName)) {
            //判断密码是否正确
            if (MD5Util.MD5(password).equals(realPass)) {
                String token = jwtConfig.createToken(loginName);
                return AjaxResult.success(token);
            } else {
                return AjaxResult.error("用户名或者密码错误");
            }
        } else {
            return AjaxResult.error("该账号还未注册，" + loginName);
        }
    }


    @PostMapping("logout")
    public  AjaxResult logout(){
        return AjaxResult.success("模拟退出，成功");
    }
}
